<?php

namespace App\Console\Commands\Tools\Product;

use App\Kuafu\Product\Model\Product;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Artisan;

class RefreshProductColumnsCommand extends Command
{
    protected $signature = 'tools:refresh-product-columns';

    protected $description = '根据菜品库原数据刷新所有菜品数据';

    public function handle()
    {
        $columns = [
            'desc'
        ];

        // 获取菜品库的数据
        $list = Product::query()
                       ->where('menuId', 0)
                       ->where('shopId', 0)
                       ->where('type', 0)
                       ->where('kind', 1)
                       ->where('status', 0)
                       ->where('updatedAt', '>=', 1701878400)
//                       ->where('updatedAt', '<=', 1701964800)
                       ->get();

        foreach ($list as $value) {
            $updateField = Arr::only($value->toArray(), $columns);

            Product::query()
                   ->where('id', $value->id)
                   ->where('menuId', '!=', 0)
                   ->update(
                       $updateField
                   );
        }


        // 清楚所有缓存
        Artisan::call('tools:refresh-menus-cache');
    }

}
